package com.fanghuaman.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fanghuaman.model.common.vo.ResponseResult;
import com.fanghuaman.model.product.dto.ProductPageDto;
import com.fanghuaman.model.product.pojo.Product;
import com.fanghuaman.model.product.vo.ProductPageVo;
import com.fanghuaman.product.mapper.ProductMapper;
import com.fanghuaman.product.service.ProductService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author smh
 * @since 2024-12-08
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public ResponseResult list(ProductPageDto productPageDto) {
        // 参数校验
        productPageDto.check();
        // 业务逻辑
        Page<Product> productPage = new Page<>(productPageDto.getPage(), productPageDto.getSize());
        // 构建查询条件
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        if (productPageDto.getProductName() != null && !productPageDto.getProductName().isEmpty()) {
            wrapper.like(Product::getName, productPageDto.getProductName());
        }
        // 根据分类id查询
        if (productPageDto.getCategoryIdMax() != null) {
            wrapper.eq(Product::getCategoryIdMax, productPageDto.getCategoryIdMax());
        }
        if (productPageDto.getCategoryIdMin() != null) {
            wrapper.eq(Product::getCategoryIdMin, productPageDto.getCategoryIdMin());
        }
        if (productPageDto.getStatus() != null) {
            wrapper.eq(Product::getStatus, productPageDto.getStatus());
        }
        productPage = this.page(productPage, wrapper);

        // 封装返回数据
        ProductPageVo productPageVo = new ProductPageVo();
        productPageVo.setTotal(productPage.getTotal());
        productPageVo.setList(productPage.getRecords());
        productPageVo.setPage(productPageDto.getPage());
        productPageVo.setSize(productPageDto.getSize());

        return ResponseResult.success(productPageVo);
    }
}
